'''
Author: thinktanker thinktanker@163.com
Date: 2024-02-06 17:21:27
LastEditors: thinktanker thinktanker@163.com
LastEditTime: 2024-02-28 15:45:15
FilePath: \duls-wechat\getsq.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import sqlite3
import time
import locale

locale.setlocale(locale.LC_CTYPE, 'chinese')

db_path = r'F:\gitee\control-net\data\hisdata.db'
ename_list = ["35kV","zhb","sb","6kV","zb","1","2","3","4"]
realname_list = ["35kV","主变","所变","6 kV","站变","1#  ","2#  ","3#  ","4#  "]
def getsq(gcname, gcstart, gcend):
    dbpath = 'F:\lljdata\swdb.db'
    print(dbpath)
    conn = sqlite3.connect(dbpath)
    c = conn.cursor()
    sq = ""
    if gcstart != "":
        tmps = f"{gcstart[0:4]}-{gcstart[4:6]}-{gcstart[6:8]}"
        tmpe = tmps
        gcstart = f"{tmps} 00:00:00"
        if gcend == "":
            gcend = f"{tmpe} 23:59:59"
        else:
            tmpe = f"{gcend[0:4]}-{gcend[4:6]}-{gcend[6:8]}"
            gcend = f"{tmpe} 23:59:59"

        # sqltxt = f"select name,sysw,xysw,time from swdb where swbdb.name like %'{gcname}'% order by time desc limit 4"
        sqltxt = f"""select name,sysw,xysw,time from swdb where name like "%{gcname}%" and time between "{gcstart}" and "{gcend}" order by time desc limit 4"""
    else:
        sqltxt = f"""select name,sysw,xysw,time from swdb where name like "%{gcname}%" order by time desc limit 4"""
    print(sqltxt)
    rs = c.execute(sqltxt)
    for row in rs:
        sq += (str(row[0]) + '\n' + str(row[3]) + "\n上游：" + str(row[1]) +
               " 下游：" + str(row[2]) + "\n\n")
    c.close()
    conn.close()
    return sq

def getgq_1zhan():
    result = []
    conn = sqlite3.connect(db_path)
    c = conn.cursor()
    sqltxt = "SELECT sysw,xysw,sqsw,ssll,pjll,time FROM swll ORDER BY time DESC LIMIT 1"
    rs = c.execute(sqltxt)
    rf = rs.fetchall()
    if rf:
        result.append(time.strftime(r"%y年%m月%d日 %H:%M:%S",time.localtime(rf[0][5]))+"\n")
        result.append(f'上游水位:{rf[0][0]}')
        result.append(f'下游水位:{rf[0][1]}')
        result.append(f'栅前水位:{rf[0][2]}')
        result.append( f'瞬时流量:{rf[0][3]}\n')
        # result.append( f'平均流量：{rf[0][4]}')
    
    for index,ename in enumerate(ename_list):
        sqltxt = f"""SELECT Uab,Ubc,Ucb,Ia,Ib,Ic,P,Q,cos,time FROM dianyadianliu WHERE ename="{ename}" ORDER BY time DESC LIMIT 1"""
        rs = c.execute(sqltxt)
        rf = rs.fetchall()
        if rf:
            result.append(realname_list[index])
            result.append(f'Uab:{rf[0][0]}')
            result.append(f'Ubc:{rf[0][1]}')
            result.append(f'Ucb:{rf[0][2]}')
            result.append(f'Ia:{rf[0][3]}')
            result.append(f'Ib:{rf[0][4]}')
            result.append(f'Ic:{rf[0][5]}')
            result.append(f'P:{rf[0][6]}')
            result.append(f'Q:{rf[0][7]}')
            result.append(f'cos:{rf[0][8]}\n')
            # result.append(f'time:{rf[0][9]}')
    # result.append(time.strftime(r"%y年%m月%d日 %H:%M:%S",time.localtime(rf[0][9]))+"\n")
    c.close()
    conn.close()
    return ' '.join(result)


if __name__ == "__main__":
    # print(getgq)
    print((getgq_1zhan()))